package route

import (
	"fmt"
	"net/http"
	"pdocker/sched"

	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

func CreateSched(c *gin.Context) {
	log.Info("CreateSched")
	var table sched.TaskTable
	if err := c.ShouldBindJSON(&table); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	log.Info("CreateSched #2")
	if err := table.Create(); err != nil {
		log.Fatal(err)
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	log.Info("CreateSched #3")
	log.WithFields(log.Fields{
		"timestamp": table.TimeStamp,
		"callback":  table.CallBack,
		"taskid":    table.TaskID,
	}).Info("任务初始化")
	go table.Start()

}

func UpdateSched(c *gin.Context) {
	log.Info("UpdateSched")
	table := new(sched.TaskTable)
	err := c.BindJSON(table)
	fmt.Println(table)
	var t = &sched.TaskTable{TaskID: table.TaskID}

	t.Update(table.TimeStamp)
	c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})

}
